package com.zwf.service.impl;

import com.zwf.context.BaseContext;
import com.zwf.dto.ShoppingCartDTO;
import com.zwf.entity.Dish;
import com.zwf.entity.Setmeal;
import com.zwf.entity.ShoppingCart;
import com.zwf.mapper.DishMapper;
import com.zwf.mapper.SetmealMapper;
import com.zwf.mapper.ShoppingCartMapper;
import com.zwf.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author zwf
 * @date 2024/2/10 14:27
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加商品到购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //判断购物车中是否存在当前添加的商品
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果存在，只需将数量加1
        if(list!=null&&list.size()>0){
            ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumber(cart);
        }else {

            //如果不存在，需要插入一条购物车数据
            //将菜品加入购物车
            if(shoppingCartDTO.getDishId()!=null){
                Dish dish = dishMapper.queryById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //将套餐加入购物车
            else {
                Setmeal setmeal=setmealMapper.queryById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.add(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList=shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //如果该菜品or套餐的数量大于1，则数量减1
        ShoppingCart sCart=shoppingCartMapper.list(shoppingCart).get(0);
        Integer number =sCart.getNumber();

        if(number>1){
            ShoppingCart shoppingCart1=new ShoppingCart();
            shoppingCart1.setId(sCart.getId());
            shoppingCart1.setUserId(userId);
            shoppingCart1.setNumber(number-1);
            shoppingCartMapper.updateNumber(shoppingCart1);
        }
        //如果该菜品or套餐的数量等于1，则删除该条记录
        else {
            shoppingCartMapper.delete(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }
}
